package com.cms.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.cms.manager.ChannelPercentManager;
import com.cms.model.AppVersionInfo;
import com.cms.model.ChannelPercent;
import com.cms.model.ImportUserChannelData;
import com.cms.model.ImportUserData;
import com.cms.service.AppVersionInfoServiceImpl;
import com.cms.service.ImportUserDataService;
import com.cms.service.ImportUserDataServiceImpl;
import com.util.BaseEntity;
import com.util.Util;

public class NFDFlightDataTimerTask extends TimerTask {

	private static Logger log = Logger.getLogger(NFDFlightDataTimerTask.class);
	private ImportUserDataService service = new ImportUserDataServiceImpl();
	private ChannelPercentManager manager = ChannelPercentManager.getInstance();
	private List<AppVersionInfo> appvlist=null;
	private int i=0;
	 
	 @Override
	 public void run() {
	  try {
	   //在这里写你要执行的内容
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -1);
		i++;
		int day = 20140923;//Integer.parseInt(Util.dateToString(c.getTime(), "yyyyMMdd"));
		appvlist = new AppVersionInfoServiceImpl().getAppVersionInfos();
	    init(day);
	  } catch (Exception e) {
	   log.info("-------------解析信息发生异常--------------"+e.getMessage());
	  }
	 }
	 
	 public void init(int day){
		 /*List<ImportUserData> usernumlist = service.getImportUserDataUserNumByDay(day,day);
		 List<ImportUserData> feelist = service.getImportUserDataFeeByDay(day,day);
		 //匹配Cpa的用户数 收入
		 List<ImportUserData> list = new ArrayList<ImportUserData>();
		 for (ImportUserData usernum_item : usernumlist) {
			for (ImportUserData fee_item : feelist) {
				if(usernum_item.getDay()==fee_item.getDay() && 
						usernum_item.getAppversionnumber().equals(fee_item.getAppversionnumber()) &&
						usernum_item.getChannelid()==fee_item.getChannelid() &&
						usernum_item.getOperator().equals(fee_item.getOperator())){
					usernum_item.setFee(fee_item.getFee());
					break;
				}
			}
			//set appnumber
			for (AppVersionInfo appv : appvlist) {
				if(appv.getAppversionnumber().equals(usernum_item.getAppversionnumber())){
					usernum_item.setAppnumber(appv.getAppnumber());
					usernum_item.setDatafileid(Integer.valueOf(String.valueOf(day)+i));
					break;
				}
			}
			if(usernum_item.getFee()>0){				
				list.add(usernum_item);
			}
		}
		 
		 System.out.println("================UserNumAndFee=================");
		 for (ImportUserData importUserData : list) {
			System.out.println(BaseEntity.toString(importUserData));
		}
		//if(service.saveImportUserData(list)) 
		{
			//匹配 渠道 扣量 用户数
			List<ImportUserChannelData> chlist = new ArrayList<ImportUserChannelData>();
			List<ChannelPercent> chplist = manager.getChannelPercents();
			for (ImportUserData item : list) {
				double usersum=item.getSum();
				for (ChannelPercent channelPercent : chplist) {
					if(item.getChannelid()==channelPercent.getChannelid() &&
							item.getAppversionnumber().equals(channelPercent.getAppversionnumber())){
						
						item.setSum1(item.getSum());
						usersum = usersum * (channelPercent.getDaypercent() / 100d);
						item.setSum((int)usersum);
						chlist.add(new ImportUserChannelData(item));
					}
				}
			}
			System.out.println("================UserNum=================");
			 for (ImportUserChannelData it : chlist) {
				System.out.println(BaseEntity.toString(it));
			 }*/
			//service.saveImportUserChannelData(chlist);
		 //}
	 }
}
	 
	
